package client.bugreport;

import client.common.dto.BugReportDto;
import client.common.result.Result;
import remote.service.UserService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

public class BugReporting extends JFrame {
    private final UserService userService = new UserService() {
    };
    private JTextArea descriptionTextArea;
    private JTextField contactDetailsField;
    private JSpinner timeSpinner; // 日期时间选择器
    private JButton submitButton;
    private JButton cancelButton;
    
    public BugReporting() {
        setTitle("Bug反馈");
        setSize(600, 400); // 初始大小
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        
        // 设置对话框的布局
        setLayout(new BorderLayout());
        
        // 创建面板用于输入bug报告内容
        JPanel inputPanel = new JPanel(new GridLayout(4, 1));
        
        // 创建描述文本框
        descriptionTextArea = new JTextArea();
        descriptionTextArea.setLineWrap(true);
        descriptionTextArea.setWrapStyleWord(true);
        JScrollPane scrollPane = new JScrollPane(descriptionTextArea);
        inputPanel.add(new JLabel("Bug描述:"));
        inputPanel.add(scrollPane);
        
        // 创建联系方式文本框
        contactDetailsField = new JTextField();
        inputPanel.add(new JLabel("联系邮箱:"));
        inputPanel.add(contactDetailsField);
        
        // 创建时间选择器
        inputPanel.add(new JLabel("Bug发生时间:"));
        timeSpinner = new JSpinner(new SpinnerDateModel());
        JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(timeSpinner, "yyyy-MM-dd HH:mm");
        timeSpinner.setEditor(timeEditor);
        timeSpinner.setValue(new Date()); // 设置当前时间
        inputPanel.add(timeSpinner);
        
        add(inputPanel, BorderLayout.CENTER);
        
        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        submitButton = new JButton("Submit");
        cancelButton = new JButton("Cancel");
        
        // 添加按钮事件
        submitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                submitBugReport();
            }
        });
        
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        
        buttonPanel.add(submitButton);
        buttonPanel.add(cancelButton);
        add(buttonPanel, BorderLayout.SOUTH);
    }
    
    public static void main(String[] args) {
        // 设置Nimbus外观
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        SwingUtilities.invokeLater(() -> {
            BugReporting frame = new BugReporting();
            frame.setVisible(true);
        });
    }
    
    // 这里是打开 BugReportDialog 的方法
    public static void openBugReportDialog() {
        SwingUtilities.invokeLater(() -> {
            BugReporting dialog = new BugReporting();
            dialog.setVisible(true);
        });
    }
    
    private void submitBugReport() {
        String description = descriptionTextArea.getText();
        String contactDetails = contactDetailsField.getText();
        Date time = (Date) timeSpinner.getValue(); // 从选择器获取时间
        
        // 创建 BugReportDto 对象
        BugReportDto bugReportDto = new BugReportDto();
        bugReportDto.setDescription(description);
        bugReportDto.setContactDetails(contactDetails);
        bugReportDto.setTime(time);
        
        // 调用用户服务的 bugReport 方法
        Result result = userService.bugReport(bugReportDto);
        if (result.isSuccess()) {
            JOptionPane.showMessageDialog(this, "Bug反馈提交成功.", "Success", JOptionPane.INFORMATION_MESSAGE);
            dispose();
        } else {
            JOptionPane.showMessageDialog(this, "提交失败: " + result.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
}